-- SPDX-License-Identifier: GPL-3.0-or-later

local extension = Package("ling4")
extension.extensionName = "lingling"

local U = require "packages/utility/utility"

local liubei = General(extension, "lingling__liubei", "shu", 4)
local rende = fk.CreateTriggerSkill{
  name = "lingling__rende",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      local targets = {}
      for _, move in ipairs(data) do
        if move.from then
          local to = player.room:getPlayerById(move.from)
          if to:isKongcheng() then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand then
                table.insertIfNeed(targets, move.from)
              end
            end
          end
        end
      end
      if #targets ~= 0 then
        self.cost_data = targets
        return true
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local targets = table.simpleClone(self.cost_data)
    room:sortPlayersByAction(targets)
    for _, target_id in ipairs(targets) do
      if not player:hasSkill(self) then break end
      local skill_target = room:getPlayerById(target_id)
      if skill_target and not skill_target.dead then
        if skill_target ~= player and not player:isNude() then
          self:doCost(event, skill_target, player, data)
        elseif skill_target == player then
          self:doCost(event, skill_target, player, data)
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if target == player then
      return room:askForSkillInvoke(player, self.name, nil, "#lingling__rende-draw")
    else
      local cards =room:askForCard(player,1,1,true,self.name,true,".","#lingling__rende-give:"..target.id)
      if #cards ~= 0 then
        self.cost_data = cards
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if target == player then
      player:drawCards(2,self.name)
    else
      room:moveCardTo(self.cost_data, Card.PlayerHand, target, fk.ReasonGive, self.name, nil, false, player.id)
    end
  end
}
local guihai = fk.CreateTriggerSkill{
  name = "lingling__guihai",
  on_acquire = function(self, player, isstart)
    local room = player.room
    table.forEach(room:getOtherPlayers(player), function(p)
      room:handleAddLoseSkills(p, "lingling__guihai&", nil, false, true)
    end)
  end,
  on_lose = function(self, player, isdead)
    local room = player.room
    local main = table.find(room.alive_players, function(p)
      return p:hasSkill(self, true)
    end)
    if not main then
      table.forEach(room:getAlivePlayers(), function(p)
        room:handleAddLoseSkills(p, "-lingling__guihai&", nil, false, true)
      end)
    end
  end
}
local guihai__other = fk.CreateViewAsSkill{
  name = "lingling__guihai&",
  pattern = "analeptic",
  prompt = "#lingling__guihai__other",
  card_filter = function (self, player, to_select, selected)
    return #selected < 2
  end,
  view_as = function(self, player, cards)
    if #cards ~= 2 then return end
    self.cost_data = cards
    local card = Fk:cloneCard("analeptic")
    card.skillName = self.name
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    local to
    local targets = table.filter(room:getOtherPlayers(player), function(p)
      return p:hasSkill("lingling__guihai")
    end)
    if #targets == 1 then
      to = targets[1]
    elseif #targets > 1 then
      to = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#lingling__guihai-give", self.name, false)
    end
    if to then
      room:moveCardTo(self.cost_data, Card.PlayerHand, to, fk.ReasonGive, self.name, nil, false, player.id)
    else
      return self.name
    end
  end,
  enabled_at_play = function (self, player)
    return player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 and
      table.find(Fk:currentRoom().alive_players, function (p)
        return p ~= player and p:hasSkill("lingling__guihai")
      end)
  end,
  enabled_at_response = function (self, player, response)
    if response or player:usedSkillTimes(self.name, Player.HistoryTurn) > 0 then return end
    return table.find(Fk:currentRoom().alive_players, function (p)
      return p ~= player and p:hasSkill("lingling__guihai")
    end)
  end,
}
Fk:loadTranslationTable{
  ["lingling__liubei"] = "刘备",
  ["#lingling__liubei"] = "仁风遗烈",
  ["illustrator:lingling__liubei"] = "伊达未来",
  ["designer:lingling__liubei"] = "伶",

  ["lingling__rende"] = "仁德",
  [":lingling__rende"] = "当一名角色失去最后的手牌后，你可以交给其一张牌，若为你失去则你摸两张牌。",
  ["lingling__guihai"] = "归海",
  [":lingling__guihai"] = "其他角色每回合限一次，其可以交给你两张牌视为使用【酒】。"..
  "<br><br> <font color = '#a40000'>声沧终泄，神光难复。",
  ["#lingling__rende-draw"] = "仁德：摸两张牌",
  ["#lingling__rende-give"] = "仁德：交给 %src 一张牌",
  ["#lingling__guihai-give"] = "归海：选择交给的目标",
  ["#lingling__guihai__other"] = "归海：你交给刘备两张牌，视为使用【酒】",
  ["lingling__guihai&"] = "归海",
  [":lingling__guihai&"] = "每回合限一次，你可以交给刘备两张牌，视为使用【酒】。"
}

local zhouyu = General(extension, "lingling__zhouyu", "wu", 3)
local yingzi = fk.CreateTriggerSkill{
  name = "lingling__yingzi",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    for _, move in ipairs(data) do
      if move.to == player.id and move.toArea == Player.Hand and move.skillName ~= self.name and
        player:getMark("lingling__yingzi-turn") < 2 then
        return true
      elseif move.from == player.id and move.to and move.to ~= player.id and player.room:getPlayerById(move.to).hp >= player.hp and
        move.toArea == Player.Hand then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand then
            return true
          end
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.to == player.id and move.toArea == Player.Hand and move.skillName ~= self.name and
        player:getMark("lingling__yingzi-turn") < 2 and player:hasSkill(self) then
        self.cost_data = 1
        self:doCost(event, target, player, data)
        break
      end
    end

    local flag  = 0
    local targets = {}
    for _, move in ipairs(data) do
      if move.from == player.id and move.to and move.to ~= player.id then
        table.insertIfNeed(targets , move.to)
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand then
            if #room:getCardsByArea(info.fromArea, player) == 0 then
              flag = 3
            else
              flag = 2
            end
          end
        end
      end
    end
    if #targets ~= 0 and flag ~= 0 then
      room:sortPlayersByAction(targets)
      for _, id in ipairs(targets) do
        if not player:hasSkill(self) then return end
        local to = room:getPlayerById(id)
        if not to.dead and to.hp >= player.hp then
          self.cost_data = flag
          self:doCost(event, to, player, data)
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if self.cost_data == 1 then
      return true
    elseif self.cost_data == 2 then
      local cards = room:askForDiscard(player, 1, 1, true, self.name, true, ".", "#lingling__yingzi-discard:"..target.id, true)
      if #cards > 0  then
        self.cost_data = cards
        return true
      end
    else
      return room:askForSkillInvoke(player, self.name, nil, "#lingling__yingzi-invoke:"..target.id)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if self.cost_data == 1 then
      room:addPlayerMark(player, "lingling__yingzi-turn", 1)
      player:drawCards(1, self.name)
    else
      if self.cost_data ~= 2 then
        room:throwCard(self.cost_data, self.name, player, player)
      end
      room:loseHp(target, 1, self.name)
    end
  end
}
local fanjian = fk.CreateActiveSkill{
  name = "lingling__fanjian",
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = "#lingling__fanjian",
  can_use = function(self, player)
    return not player:isNude() and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local id = room:askForCardChosen(target, player, "he", self.name)
    room:moveCardTo(id, Card.PlayerHand, target, fk.ReasonPrey, self.name, nil, false, target.id)
    if not player.dead and not target.dead and not target:isNude() then
      local card = room:askForCard(target, 1, 1, true, self.name, false, ".", "#lingling__fanjian-give:"..player.id)
      room:moveCardTo(card, Player.Hand, player, fk.ReasonGive, self.name, nil, false, target.id)
    end
  end
}
zhouyu:addSkill(yingzi)
zhouyu:addSkill(fanjian)
Fk:loadTranslationTable{
  ["lingling__zhouyu"] = "周瑜",
  ["#lingling__zhouyu"] = "江左风流",
  ["illustrator:lingling__zhouyu"] = "伊达未来",
  ["designer:lingling__zhouyu"] = "伶",

  ["lingling__yingzi"] = "英姿",
  [":lingling__yingzi"] = "当你每回合前两次不因此技能获得牌后，你摸一张牌。当其他角色获得你的牌后，若其体力值不少于你，你可以弃置一张牌"..
  "令其失去1点体力，若获得你最后的手牌或装备区最后的牌则无须弃置牌。",
  ["lingling__fanjian"] = "反间",
  [":lingling__fanjian"] = "出牌阶段限一次，你可以令一名其他角色获得你一张牌，然后其交给你一张牌。"..
  "<br><br> <font color = '#a40000'>文武筹略，万人之英。",
  ["#lingling__yingzi-discard"] = "英姿：弃置一张牌,令%src失去一点体力",
  ["#lingling__yingzi-invoke"] = "英姿：令%src失去一点体力",
  ["#lingling__fanjian"] = "反间：令一名角色获得你一张牌，然后其交给你一张牌",
  ["#lingling__fanjian-give"] = "反间：请交给 %src 一张牌"
}

local xunyu = General(extension, "lingling__xunyu", "wei", 3)
local quhu = fk.CreateActiveSkill{
  name = "lingling__quhu",
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return not player:isKongcheng() and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    local target = Fk:currentRoom():getPlayerById(to_select)
    return #selected == 0 and player:canPindian(target) and target.hp > player.hp
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local pindian = player:pindian({target}, self.name)
    if pindian.results[target.id].winner == player then
      local targets = {}
      for _, p in ipairs(room:getOtherPlayers(target)) do
        if target:inMyAttackRange(p) then
          table.insert(targets, p.id)
        end
      end
      if #targets == 0 then return end
      local tos = room:askForChoosePlayers(player, targets, 1, 1, "#quhu-choose", self.name)
      room:damage{
        from = target,
        to = room:getPlayerById(tos[1]),
        damage = 1,
        skillName = self.name,
      }
    else
      room:damage{
        from = target,
        to = player,
        damage = 1,
        skillName = self.name,
      }
    end
  end,
}
local jieming = fk.CreateTriggerSkill{
  name = "lingling__jieming",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for i = 1, data.damage do
      if i > 1 and (self.cancel_cost or not player:hasSkill(self)) then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1,
      "#lingling__jieming-choose", self.name, true)
    if #to > 0 then
      self.cost_data = {tos = to}
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    to:drawCards(math.min(to.maxHp, 5), self.name)
    if to.dead then return false end
    local x = to:getHandcardNum() - math.min(to.maxHp, 5)
    if x > 0 then
      room:askForDiscard(to, x, x, false, self.name, false, nil, "#lingling__jieming-discard:::"..x)
    end
  end,
}
Fk:loadTranslationTable{
  ["lingling__xunyu"] = "荀彧",
  ["illustrator:lingling__xunyu"] = "伊达未来",
  --["designer:lingling__xunyu"] = "伶",

  ["lingling__quhu"] = "驱虎",
  [":lingling__quhu"] = "出牌阶段限一次，你可以与一名体力大于你的角色拼点：若你赢，其对其攻击范围内另一名由你选择的角色造成1点伤害；"..
  "若你没赢，其对你造成1点伤害。",
  ["lingling__jieming"] = "节命",
  [":lingling__jieming"] = "当你受到1点伤害后，你可以令一名角色摸X张牌，然后将手牌弃至X张（X为其体力上限且至多为5）。"..
  "<br><br> <font color = '#a40000'>既为张子房，何乃阻九锡。",
  ["#lingling__jieming-choose"] = "节命：你可以令一名角色摸X张牌并将手牌弃至X张（X为其体力上限且至多为5）",
  ["#lingling__jieming-discard"] = "节命：选择%arg张手牌弃置",
}

local luxun = General(extension, "lingling__luxun", "wu", 4)
local shenjun = fk.CreateViewAsSkill{
  name = "lingling__shenjun",
  pattern = "fire_attack,nullification",
  mute = true,
  prompt = function (self, selected_cards, selected)
    if player:getHandcardNum() ~= 2 then
      return "#lingling__shenjun"
    else
      return "#lingling__shenjun-draw"
    end
  end,
  times = function(self)
    return 2 - player:usedSkillTimes(self.name, Player.HistoryTurn)
  end,
  interaction = function(self)
    if player:getHandcardNum() ~= 2 then
      local all_names = {"fire_attack", "nullification"}
      local names = player:getViewAsCardNames(player, self.name, all_names)
      if #names > 0 then
        return UI.CardNameBox {choices = names, all_choices = all_names}
      end
    else
      return UI.CardNameBox {
        choices = {"lingling__shenjun_draw"},
        default_choice = "lingling__shenjun_draw",
      }
    end
  end,
  card_filter = function (self, player, to_select, selected)
    if player:getHandcardNum() > 2 then
      return #selected < player:getHandcardNum() - 2 and table.contains(player:getCardIds("h"), to_select) and
        not player:prohibitDiscard(to_select)
    else
      return false
    end
  end,
  view_as = function(self, player, cards)
    if player:getHandcardNum() > 2 then
      if #cards ~= player:getHandcardNum() - 2 then return end
      self.cost_data = cards
    else
      if #cards > 0 then return end
      self.cost_data = nil
    end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = self.name
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    if self.interaction.data == "fire_attack" then
      room:notifySkillInvoked(player, self.name, "offensive")
    elseif self.interaction.data == "nullification" then
      room:notifySkillInvoked(player, self.name, "control")
    elseif self.interaction.data == "lingling__shenjun_draw" then
      room:notifySkillInvoked(player, self.name, "drawcard")
    end
    if self.cost_data then
      room:throwCard(self.cost_data, self.name, player, player)
    elseif player:getHandcardNum() < 2 then
      player:drawCards(2 - player:getHandcardNum(), self.name)
    elseif player:getHandcardNum() == 2 then
      player:drawCards(1, self.name)
      return ""
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryTurn) < 2
  end,
  enabled_at_response = function(self, player, response)
    return not response and player:getHandcardNum() ~= 2 and player:usedSkillTimes(self.name, Player.HistoryTurn) < 2
  end,
}
luxun:addSkill(shenjun)
Fk:loadTranslationTable{
  ["lingling__luxun"] = "陆逊",
  ["#lingling__luxun"] = "剑照儒纶",
  ["illustrator:lingling__luxun"] = "伊达未来",
  ["designer:lingling__luxun"] = "屑",

  ["lingling__shenjun"] = "神君",
  [":lingling__shenjun"] = "每回合限两次，若你的手牌数不为2，你可以将手牌数调整至2，视为使用【火攻】或【无懈可击】；出牌阶段，若你的手牌数为2，"..
  "你可以摸一张牌。<br><br> <font color = '#a40000'>虎卧国门，威深大江。",
  ["#lingling__shenjun"] = "神君：你可以将手牌数调整至2，视为使用【火攻】或【无懈可击】",
  ["#lingling__shenjun-draw"] = "神君：你可以摸一张牌",
}

local simayi = General(extension, "lingling__simayi", "wei", 3)
local chaobing = fk.CreateTriggerSkill{
  name = "lingling__chaobing",
  anim_type = "control",
  events = {fk.AskForRetrial, fk.SkillEffect},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if event == fk.AskForRetrial then
        return not player:isNude()
      elseif event == fk.SkillEffect then
        if target and target ~= player and not table.contains(player:getTableMark("lingling__chaobing-turn"), target.id) and
          target:hasSkill(data, true) and data:isPlayerSkill(target) and not data.name:startsWith("#") then
          player.room:addTableMark(player, "lingling__chaobing-turn", target.id)
          return player:getMark("lingling__chaobing-round") == 0
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AskForRetrial then
      local card = room:askForCard(player, 1, 1, true, self.name, true, nil,
        "#lingling__chaobing-ask::"..target.id..":"..data.reason)
      if #card > 0 then
        self.cost_data = {cards = card}
        return true
      end
    elseif event == fk.SkillEffect then
      if room:askForSkillInvoke(player, self.name, nil, "#lingling__chaobing-invoke::"..target.id..":"..data.name) then
        self.cost_data = {tos = {target.id}}
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AskForRetrial then
      local card = Fk:getCardById(self.cost_data.cards[1])
      room:retrial(card, player, data, self.name, true)
    elseif event == fk.SkillEffect then
      room:setPlayerMark(player, "lingling__chaobing-round", 1)
      local judge = {
        who = player,
        reason = self.name,
        pattern = ".|.|spade",
      }
      room:judge(judge)
      if judge.card.suit == Card.Spade then
        room:invalidateSkill(target, data.name, "-turn")
        if data.main_skill then
          room:invalidateSkill(target, data.main_skill.name, "-turn")
        end
        local e = room.logic:getCurrentEvent():findParent(GameEvent.SkillEffect)
        if e and e.data[3].name == data.name then
          e:shutdown()
        end
      end
    end
  end,
}
local langgu = fk.CreateTriggerSkill{
  name = "lingling__langgu",
  anim_type = "control",
  events = {fk.TargetSpecified, fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and data.card.trueName == "slash" then
      if event == fk.TargetSpecified then
        return player:getHandcardNum() <= player.room:getPlayerById(data.to):getHandcardNum()
      elseif event == fk.TargetConfirmed then
        return player:getHandcardNum() <= player.room:getPlayerById(data.from):getHandcardNum()
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local prompt = "#lingling__langgu-invoke::"..data.to
    if event == fk.TargetConfirmed then
      prompt = "#lingling__langgu-invoke::"..data.from
    end
    return player.room:askForSkillInvoke(player, self.name, nil, prompt)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local judge = {
      who = player,
      reason = self.name,
      pattern = ".|.|spade,club",
      skipDrop = true,
    }
    room:judge(judge)
    if not player.dead and judge.card.color == Card.Black then
      local to
      if event == fk.TargetSpecified then
        to = room:getPlayerById(data.to)
      elseif event == fk.TargetConfirmed then
        to = room:getPlayerById(data.from)
      end
      local choices = {}
      if not to.dead and not to:isNude() and to ~= player then
        table.insert(choices, "lingling__langgu_prey::"..to.id)
      end
      if room:getCardArea(judge.card) == Card.Processing then
        table.insert(choices, "lingling__langgu_get")
      end
      if #choices > 0 then
        local choice = room:askForChoice(player, choices, self.name)
        if choice == "lingling__langgu_get" then
          room:moveCardTo(judge.card, Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, true, player.id)
        else
          if room:getCardArea(judge.card) == Card.Processing then
            room:moveCardTo(judge.card, Card.DiscardPile, nil, fk.ReasonJudge)
          end
          local card = room:askForCardChosen(player, to, "he", self.name, "#lingling__langgu-prey::"..to.id)
          room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, self.name, nil, false, player.id)
        end
      end
    else
      room:moveCardTo(judge.card, Card.DiscardPile, nil, fk.ReasonJudge)
    end
  end,
}
simayi:addSkill(chaobing)
simayi:addSkill(langgu)
Fk:loadTranslationTable{
  ["lingling__simayi"] = "司马懿",
  ["#lingling__simayi"] = "洛水无声",
  ["illustrator:lingling__simayi"] = "伊达未来",
  ["designer:lingling__simayi"] = "伶",

  ["lingling__chaobing"] = "朝柄",
  [":lingling__chaobing"] = "当一名角色的判定牌生效前，你可以用一张牌替换之。每轮限一次，其他角色每回合首次发动技能时，你可以判定，"..
  "♠则停止其该技能结算，且其该技能本回合无效。",
  ["lingling__langgu"] = "狼顾",
  [":lingling__langgu"] = "当你使用或被使用【杀】后，若对方手牌不少于你，你可以判定，若为黑色，你获得对方一张牌或判定牌。"..
  "<br><br> <font color = '#a40000'>六朝何事，门户私计。",
  ["#lingling__chaobing-ask"] = "朝柄：你可以用一张牌替换 %dest 的“%arg”判定",
  ["#lingling__chaobing-invoke"] = "朝柄：%dest 发动“%arg”，是否进行判定，若为♠则其技能终止结算且本回合无效",
  ["#lingling__langgu-invoke"] = "狼顾：是否进行判定？若为黑色，你可以获得 %dest 一张牌或获得判定牌",
  ["lingling__langgu_prey"] = "获得 %dest 一张牌",
  ["lingling__langgu_get"] = "获得判定牌",
  ["#lingling__langgu-prey"] = "狼顾：获得 %dest 一张牌",
}

local zhangjiao = General(extension, "lingling__zhangjiao", "qun", 3)
local juyi = fk.CreateTriggerSkill{
  name = "lingling__juyi",
  anim_type = "special",
  frequency = Skill.Compulsory,
  events = {fk.AfterCardsMove, fk.DamageInflicted, fk.RoundEnd, fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) or player:getMark("@ling_daoxin") > 9 then return end
    if event == fk.AfterCardsMove then
      for _, move in ipairs(data) do
        if move.from and player.room:getPlayerById(move.from):isKongcheng() then
          local turn_event = player.room.logic:getCurrentEvent():findParent(GameEvent.Turn)
          if turn_event == nil or turn_event.data[1].id ~= move.from then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand then
                return true
              end
            end
          end
        end
      end
    elseif event == fk.DamageInflicted then
      return data.damage >= target.hp + target.shield
    elseif event == fk.RoundEnd then
      return not table.hasIntersection(player:getTableMark("lingling__juyi-round"), {fk.AfterCardsMove, fk.DamageInflicted})
    elseif event == fk.Damaged then
      return target == player
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addTableMark(player, "lingling__juyi-round", event)
    local n = 1
    if player:getMark("@ling_daoxin") == 0 or event == fk.Damaged then
      n = 2
    end
    n = math.min(10 - player:getMark("@ling_daoxin"), n)
    room:addPlayerMark(player, "@ling_daoxin", n)
  end,
}
local tuishan = fk.CreateViewAsSkill{
  name = "lingling__tuishan",
  anim_type = "special",
  pattern = ".|.|.|.|thunder__slash,jink",
  prompt = "#lingling__tuishan2",
  interaction = function(self)
    local all_names = {"thunder__slash", "jink"}
    local names = player:getViewAsCardNames(player, self.name, all_names)
    if #names > 0 then
      return UI.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    if not self.interaction.data then return nil end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = self.name
    return card
  end,
  before_use = function(self, player)
    player.room:removePlayerMark(player, "@ling_daoxin", 2)
  end,
  enabled_at_play = function(self, player)
    return player:getMark("@ling_daoxin") > 1
  end,
  enabled_at_response = function(self, player, response)
    return player:getMark("@ling_daoxin") > 1
  end,

  on_acquire = function (self, player, is_start)
    player.room:handleAddLoseSkills(player, "lingling__tuishan&", nil, false, true)
  end,
  on_lose = function (self, player, is_death)
    player.room:handleAddLoseSkills(player, "-lingling__tuishan&", nil, false, true)
  end,
}
local tuishan_active = fk.CreateActiveSkill{
  name = "lingling__tuishan&",
  anim_type = "offensive",
  prompt = "#lingling__tuishan&",
  card_num = 0,
  min_target_num = 1,
  interaction = function()
    local choices = {}
    if player:getMark("@ling_daoxin") > 3 then
      table.insert(choices, "lingling__tuishan4")
    end
    if player:getMark("@ling_daoxin") > 5 then
      table.insert(choices, "lingling__tuishan6")
    end
    return UI.ComboBox { choices = choices, all_choices = {
      "lingling__tuishan4",
      "lingling__tuishan6",
    } }
  end,
  can_use = function(self, player)
    return player:getMark("@ling_daoxin") > 3
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    if self.interaction.data == "lingling__tuishan4" then
      return #selected < 2 and not Fk:currentRoom():getPlayerById(to_select).chained
    elseif self.interaction.data == "lingling__tuishan6" then
      return #selected == 0
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:removePlayerMark(player, "@ling_daoxin", tonumber(self.interaction.data[18]))
    if self.interaction.data == "lingling__tuishan4" then
      room:sortPlayersByAction(effect.tos)
      for _, id in ipairs(effect.tos) do
        local p = room:getPlayerById(id)
        if not p.dead and not p.chained then
          p:setChainState(true)
        end
      end
      for _, id in ipairs(effect.tos) do
        if player.dead then return end
        local p = room:getPlayerById(id)
        if not p.dead and not p:isNude() then
          if p == player then
            room:askForDiscard(player, 1, 1, true, "lingling__tuishan", false, nil, "#lingling__tuishan-discard::"..p.id)
          else
            local card = room:askForCardChosen(player, p, "he", "lingling__tuishan", "#lingling__tuishan-discard::"..p.id)
            room:throwCard(card, "lingling__tuishan", p, player)
          end
        end
      end
    elseif self.interaction.data == "lingling__tuishan6" then
      room:damage {
        from = player,
        to = room:getPlayerById(effect.tos[1]),
        damage = 2,
        skillName = "lingling__tuishan",
        damageType = fk.ThunderDamage,
      }
    end
    if not player.dead and player:getMark(self.interaction.data) == 0 then
      room:setPlayerMark(player, self.interaction.data, 1)
      player:drawCards(3, "lingling__tuishan")
    end
  end,
}
Fk:loadTranslationTable{
  ["lingling__zhangjiao"] = "张角",
  ["#lingling__zhangjiao"] = "大贤良师",
  ["illustrator:lingling__zhangjiao"] = "伊达未来",
  ["designer:lingling__zhangjiao"] = "伶",

  ["lingling__juyi"] = "聚蚁",
  [":lingling__juyi"] = "当一名角色于回合外失去最后的手牌后，或一名角色受到致命伤害时，或一轮结束时本轮未出现以上两种时机，你获得1点“道心值”，"..
  "若你没有“道心值”则额外获得1点。当你受到伤害后，你获得2点“道心值”。你的“道心值”最多10点。",
  ["lingling__tuishan"] = "推山",
  [":lingling__tuishan"] = "你可以消耗：2点“道心值”，于需要时视为使用或打出雷【杀】或【闪】；4点“道心值”，于出牌阶段令至多两名角色横置并"..
  "弃置其各一张牌；6点“道心值”，于出牌阶段对一名角色造成2点雷电伤害。你首次发动4点和6点的效果后摸三张牌。"..
  "<br><br> <font color = '#a40000'>不德以德，不安以安；羚羊食肉，蜗牛狂奔。",
  ["lingling__tuishan&"] = "推山",
  [":lingling__tuishan&"] = "你可以消耗4点“道心值”，于出牌阶段令至多两名角色横置并弃置其各一张牌；6点“道心值”，"..
  "于出牌阶段对一名角色造成2点雷电伤害。你首次发动4点和6点的效果后摸三张牌。",
  ["@ling_daoxin"] = "道心值",
  ["#lingling__tuishan2"] = "推山：你可以消耗2点“道心值”，视为使用或打出雷【杀】或【闪】",
  ["#lingling__tuishan&"] = "推山：你可以消耗“道心值”，执行对应效果",
  ["lingling__tuishan4"] = "4点，横置至多两名角色并各弃置一张牌",
  ["lingling__tuishan6"] = "6点，造成2点雷电伤害",
  ["#lingling__tuishan-discard"] = "推山：弃置 %dest 一张牌",
}
